<template>
  <el-container class="customer-layout">
    <el-header class="header">
      <div class="header-left">
        <el-icon :size="24" style="margin-right: 10px;"><ShoppingBag /></el-icon>
        <span class="logo">便利店商城</span>
      </div>
      
      <div class="header-center">
        <el-menu
          :default-active="activeMenu"
          mode="horizontal"
          :ellipsis="false"
          @select="handleMenuSelect"
        >
          <el-menu-item index="/customer/home">首页</el-menu-item>
          <el-menu-item index="/customer/products">商品浏览</el-menu-item>
          <el-menu-item index="/customer/orders">我的订单</el-menu-item>
          <el-menu-item index="/customer/refunds">退货记录</el-menu-item>
        </el-menu>
      </div>
      
      <div class="header-right">
        <el-badge :value="cartCount" :hidden="cartCount === 0">
          <el-button
            circle
            :icon="ShoppingCart"
            @click="$router.push('/customer/cart')"
          />
        </el-badge>
        
        <el-dropdown @command="handleCommand">
          <div class="user-info">
            <el-avatar :size="32">{{ userStore.userInfo?.username?.charAt(0) }}</el-avatar>
            <span>{{ userStore.userInfo?.username }}</span>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">
                <el-icon><User /></el-icon> 个人信息
              </el-dropdown-item>
              <el-dropdown-item command="logout" divided>
                <el-icon><SwitchButton /></el-icon> 退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    
    <el-main class="main">
      <router-view />
    </el-main>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useCartStore } from '@/stores/cart'
import { ElMessageBox } from 'element-plus'
import { ShoppingCart } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const cartStore = useCartStore()

const activeMenu = computed(() => {
  return route.path
})

const cartCount = computed(() => {
  return cartStore.cartCount || 0
})

const handleMenuSelect = (index) => {
  router.push(index)
}

const handleCommand = async (command) => {
  if (command === 'profile') {
    router.push('/customer/profile')
  } else if (command === 'logout') {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      type: 'warning'
    })
    await userStore.logout()
    router.push('/login')
  }
}
</script>

<style lang="scss" scoped>
.customer-layout {
  width: 100%;
  height: 100vh;
  
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    padding: 0 20px;
    
    .header-left {
      display: flex;
      align-items: center;
      
      .logo {
        font-size: 20px;
        font-weight: bold;
        color: #409eff;
      }
    }
    
    .header-center {
      flex: 1;
      display: flex;
      justify-content: center;
    }
    
    .header-right {
      display: flex;
      align-items: center;
      gap: 20px;
      
      .user-info {
        display: flex;
        align-items: center;
        gap: 10px;
        cursor: pointer;
        
        span {
          font-size: 14px;
        }
      }
    }
  }
  
  .main {
    background: #f5f5f5;
    overflow-y: auto;
  }
}
</style>
















